package de.global42.clubapp.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * @author lutz
 *
 */
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames={"code","venue_id"}))
public class ValidationCode implements Serializable{

   /**
	 *Generated Version UID 
	 */
	private static final long serialVersionUID = -7791345697213253486L;
@Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id", updatable = false, nullable = false)
   private Long id = null;
   @Version
   @Column(name = "version")
   private int version = 0;
   
   @Column(unique = true)
   @NotEmpty
   @NotNull
   protected String code;
   
   @Temporal(TemporalType.TIMESTAMP)
   protected Date devaluatedOn;
   
   @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinColumn
   protected Mission mission;
   
   @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinColumn
   private Venue venue;

   public Long getId()
   {
      return this.id;
   }

   public void setId(final Long id)
   {
      this.id = id;
   }

   public int getVersion()
   {
      return this.version;
   }

   public void setVersion(final int version)
   {
      this.version = version;
   }

   public String getCode() {
	return code;
}

public void setCode(String code) {
	this.code = code;
}

public Date getDevaluatedOn() {
	return devaluatedOn;
}

public void setDevaluatedOn(Date devaluatedOn) {
	this.devaluatedOn = devaluatedOn;
}

public Mission getMission() {
	return mission;
}

public void setMission(Mission mission) {
	this.mission = mission;
}

public Venue getVenue() {
	return venue;
}

public void setVenue(Venue venue) {
	this.venue = venue;
}

public String toString()
   {
      String result = "";
      if (id != null)
         result += id;
      return result;
   }

   @Override
   public boolean equals(Object that)
   {
      if (this == that)
      {
         return true;
      }
      if (that == null)
      {
         return false;
      }
      if (getClass() != that.getClass())
      {
         return false;
      }
      if (id != null)
      {
         return id.equals(((ValidationCode) that).id);
      }
      return super.equals(that);
   }

   @Override
   public int hashCode()
   {
      if (id != null)
      {
         return id.hashCode();
      }
      return super.hashCode();
   }

	public boolean isStillValid() {
		return devaluatedOn == null;
	}

	public void devaluate() {
		setDevaluatedOn(new Date());
	}
}